package com.ics.top.batch.management.processor;


import com.ics.top.batch.management.config.BatchUserManagementConfiguration;
import com.ics.top.batch.management.domain.BatchUserManagementDTO;
import com.ics.top.batch.management.domain.mapper.BatchUserManagementMapper;
import com.ics.top.domain.SysMember;
import com.ics.top.second.domain.UserManagement;
import com.ics.top.second.service.UserManagementService;
import com.ics.top.service.SysMemberService;
import com.ics.top.service.dto.SysMemberDTO;
import com.ics.top.service.util.DateUtil;
import com.ics.top.service.util.NumberUtil;
import com.ics.top.service.util.StirngUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2018/4/25.
 */
@Component
public class BatchUserManagementProcessor implements ItemProcessor<BatchUserManagementDTO, UserManagement> {
    private static final Logger log = LoggerFactory.getLogger(BatchUserManagementConfiguration.class);
    @Autowired
    private BatchUserManagementMapper batchItemMapper;
    @Autowired
    private UserManagementService userManagementService;

    @Override
    public UserManagement process(BatchUserManagementDTO item) throws Exception {
        log.info("获取数据BatchItemDTO {}",item);
        //获取交易id和子交易id去数据库查询
        Date time = DateUtil.getDate();
        UserManagement  obj =  batchItemMapper.toUserManagement(item);;
        //获取昨天的商品评价
        UserManagement userManagement =  userManagementService.get(item.getMobile(),item.getStoresName());
        if( userManagement != null){//修改
            if(!"1,2".equals(userManagement.getDataSources())&& !"2".equals(userManagement.getDataSources())){
                userManagement.setDataSources("1,2");
            }
            userManagement.setGroupIds(StirngUtil.stringJion(userManagement.getGroupIds(),item.getGroupIds()));
            obj = userManagement;
        }else{//添加
            obj.setCreateTime(time);
            obj.setStoresId(0);
        }
        obj.setAvgPrice(NumberUtil.getTotalAmount(item.getAvgPrice()));
        obj.setTradeAmount(NumberUtil.getTotalAmount(item.getTradeAmount()));
        obj.setCloseTradeAmount(NumberUtil.getTotalAmount(item.getCloseTradeAmount()));
        obj.setUpdateTime(time);
        obj.setLocale(item.getLocale()); //地址
/*        obj.setNickName(item.getNickName());
        obj.setName(item.getBuyerNick());*/
        log.info("转换后的数据PositiveNegativeAssessData {} ",obj);
        return  obj;
    }
}
